﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using AEM.Controls;
using AEM.Data;
using Actions = AEM.Data.BLL.Actions;

namespace AEM
{
    /// <summary>
    /// LiftInLine.xaml 的交互逻辑
    /// </summary>
    public partial class LiftInLine : Window
    {
        public event EventHandler<EventArgs> HideEventHandler;
        public LiftInLine()
        {
            InitializeComponent();
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(TitleBar_MouseLeftButtonDown);
            this.Loaded += new RoutedEventHandler(LiftInLine_Loaded);
            this.ContentRendered += new EventHandler(LiftInLine_ContentRendered);
            Actions.LiftInfo.LiftRunInLine = doLiftRunInLine;
            Actions.LiftInfo.LiftRunNCInLine = doLiftRunNCInLine;
        }

        void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void doLiftRunInLine(SerialDataEventArgs e)
        {
            if (Dispatcher.CheckAccess()) return;
            if (null == e || null == e.Data) return;
            PortMonitoring.InLineRunSign[e.LiftNum - 1] = true;

            Data.Enum.LiftStatus ls = Data.Enum.LiftStatus.Normal;
            if (e.Data.Dirver) ls = Data.Enum.LiftStatus.Driver;
            if (e.Data.Overhaul) ls = Data.Enum.LiftStatus.Overhaul;
            if (e.Data.Locked) ls = Data.Enum.LiftStatus.Locked;
            if (e.Data.Fire) ls = Data.Enum.LiftStatus.Fire;
            if (e.Data.FaultLamp || e.Data.ConverterAlarm || e.Data.CageAlarm || e.Data.IsEarthquaked) ls = Data.Enum.LiftStatus.Fault;

            Dispatcher.Invoke(new Action(() =>
            {
                LiftLine l = (LiftLine)this.lift_lines.FindName("ll" + e.LiftInfo.LiftLine);
                if (null != l)
                {
                    l.StatusChanged.BeginInvoke(e.LiftNum.ToString(), ls, new AsyncCallback(Callback), null);
                }
            }));
        }

        private void doLiftRunNCInLine(int liftnum)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                if (null == Config.LiftInftList) return;
                Data.Model.LiftInfo lift = (from i in Config.LiftInftList
                                            where i.LiftNum == liftnum
                                            select i).FirstOrDefault();
                if (null == lift) return;
                LiftLine l = (LiftLine)this.lift_lines.FindName("ll" + lift.LiftLine);
                if (null != l)
                {
                    l.StatusChanged.BeginInvoke(lift.LiftNum.ToString(), Data.Enum.LiftStatus.NC, new AsyncCallback(Callback), null);
                }
            }));
        }

        private void Callback(IAsyncResult tag)
        {
            AsyncResult result = (AsyncResult)tag;
            Action<string, Data.Enum.LiftStatus> del = (Action<string, Data.Enum.LiftStatus>)result.AsyncDelegate;
            del.EndInvoke(tag);
        }

        void LiftInLine_Loaded(object sender, RoutedEventArgs e)
        {
            this.lift_lines.Children.Remove(line1);
            IEnumerable<int> resule;

            if (null == Config.LiftInftList) return;
            resule = from Data.Model.LiftInfo row in Config.LiftInftList
                     orderby row.LiftLine
                     select row.LiftLine;

            resule = resule.Distinct();
           
            foreach (int s in resule)
            {
                LiftLine l = new LiftLine();
                this.RegisterName("ll" + s, l);
                l.Name = "ll" + s;
                l.LineName = s.ToString();
                this.lift_lines.Children.Add(l);
            }
        }

        void LiftInLine_ContentRendered(object sender, EventArgs e)
        {
            if (null != Config.LiftInftList)
                PortMonitoring.InLineRunSign = new bool[Config.LiftInftList.Select(x => x.LiftNum).Max()];
        }

        private void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            if (null != HideEventHandler)
                HideEventHandler.Invoke(this, null);
            //this.Close();
        }

        private void sys_button_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

    }
}
